package in.muton.edushare.panels;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.validator.PatternValidator;

import com.edushare.common.util.StringUtil;

public class MonthYearInputBox extends FormComponentPanel<Date> {

	private static final long serialVersionUID = 3602411968014609004L;
	private Date date;
	private String selectedMonth;
	private String selectedYear;
	private final DropDownChoice<String> monthDDC;
	private final TextField<String> yearTF;
	
	
	public static final ArrayList<String> monthList = new ArrayList<String>();
	static{
		monthList.add("January");
		monthList.add("February");
		monthList.add("March");
		monthList.add("April");
		monthList.add("May");
		monthList.add("June");
		monthList.add("July");
		monthList.add("August");
		monthList.add("September");
		monthList.add("October");
		monthList.add("November");
		monthList.add("December");
	}
	
	public MonthYearInputBox(String id) {
		this(id, null);
	}

	public MonthYearInputBox(String id, IModel<Date> model){
		super(id, model);
		
		this.setOutputMarkupId(true);
		
		PropertyModel<String> monthModel = new PropertyModel<String>(this, "selectedMonth");
		monthDDC = new DropDownChoice<String>("month", monthModel, monthList);
		add(monthDDC);
		
		PropertyModel<String> yearModel = new PropertyModel<String>(this, "selectedYear");
		yearTF = new TextField<String>("year", yearModel);
		yearTF.setRequired(true);
		yearTF.add(new PatternValidator("[1-2]\\d{3}"));
		add(yearTF);
	}
	
	@Override
	protected void onBeforeRender() {
		
		if(getModelObject()==null)
			this.date = new Date();
		else if(!(getModelObject() instanceof Date))
				throw new WicketRuntimeException("MonthYearChooser["
						                            + getPath()
						                            + "] contains an invalid model object, must be an object of type java.util.Date");
		else
			this.date = getModelObject();
		
		selectedMonth = new SimpleDateFormat("MMMM").format(this.date);
		selectedYear = new SimpleDateFormat("yyyy").format(this.date);
		
		super.onBeforeRender();
	}
	
	@Override
	protected void convertInput() {
		String lYr = yearTF.getConvertedInput();
		String lMo = monthDDC.getConvertedInput();
		
		if(StringUtil.isValid(lYr)&& StringUtil.isValid(lMo)){
		
			int monthIdx = monthList.indexOf(lMo) + 1;
			String dStr = lYr + "-" + (monthIdx<10?"0":"") + monthIdx +"-01";
			java.sql.Date lDate = java.sql.Date.valueOf(dStr);
			setConvertedInput(new Date(lDate.getTime()));
		}
		else{
			setConvertedInput(null);
		}
	}

	
}
